﻿using System.Collections.Generic;
using System.IO;
using UnityEngine;

namespace Elvex
{
    public enum AssetBundlePathType
    {
        Streaming, // 流数据
        Persistent, // 持久性数据
    }

    public static class AssetBundles
    {
        class AssetBundleMemory
        {
            /// <summary>
            /// 资源包
            /// </summary>
            private AssetBundle m_bundle;

            /// <summary>
            /// 引用计数
            /// </summary>
            private int m_referenceCount = 0;

            public AssetBundleMemory(AssetBundle bundle)
            {
                m_bundle = bundle;
                m_referenceCount = 0;
            }

            public AssetBundle Load()
            {
                ++m_referenceCount;
                return m_bundle;
            }

            public void UnLoad()
            {
                --m_referenceCount;
                if(m_referenceCount < 0 )
                {
                    m_referenceCount = 0;
                }
            }
        }

        static Dictionary<string, AssetBundleMemory> AssetBundleMemories { get; } = new Dictionary<string, AssetBundleMemory>();

        static AssetBundleManifest m_manifest;

        private static string AssetsPath { 
            get {
                return UnityEngine.Application.streamingAssetsPath;
            }
        }

        private static string ManifestName {
            get {
//#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
//                return "StandaloneWindows";
#if UNITY_EDITOR
                return UnityEditor.EditorUserBuildSettings.activeBuildTarget.ToString();
#else
                return UnityEngine.Application.platform.ToString();
#endif
            }
        }

        public static void LoadManifest()
        {
            var assetBundle = AssetBundle.LoadFromFile(Path.Combine(AssetsPath, ManifestName));
            m_manifest = assetBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");
        }

        public static AssetBundle Load(string path)
        {
            if (null == m_manifest)
            {
                LoadManifest();
            }

            string bundlename = path.ToLower();
            if (AssetBundleMemories.TryGetValue(bundlename, out var memory))
            {
                return memory.Load();
            } 
            var bundlepath = AssetManager.Instance.Configuration.GetAssetBundlePath(bundlename);
            AssetBundle bundle = UnityEngine.AssetBundle.LoadFromFile(bundlepath);
            if(null == bundle)
            {
                return null;
            }
            memory = new AssetBundleMemory(bundle);
            AssetBundleMemories.Add(bundlename, memory);

            // 加载依赖
            string[] dependencies = m_manifest.GetAllDependencies(bundlename);
            foreach(var item in dependencies)
            {
                if(null == Load(item))
                {
                    Debug.LogWarningFormat("Load[{0}}] Dependency [{1}] error!", bundlename, item);
                }
            }
            return memory.Load();
        }
    }
}
